export function getBase64(img, callback) {
  const reader = new FileReader();
  reader.addEventListener('load', () => callback(reader.result));
  reader.readAsDataURL(img);
}

// 给没有小数点的数加上.00
export function floatToString(float) {
  let nFloat = Number(float);
  if (isNaN(nFloat)) return;
  if (nFloat) {
    nFloat = nFloat.toFixed && nFloat.toFixed(2) && nFloat.toFixed(2).toString && nFloat.toFixed(2).toString();
    const s = nFloat.indexOf('.');
    if (s < 0) {
      nFloat = `${nFloat}.00`;
    }
  } else {
    nFloat = '0.00';
  }
  return nFloat;
}
// 浮点数乘法精确算法
export function accMul(arg1, arg2) {
  let m = 0;
  const s1 = arg1.toString(),
    s2 = arg2.toString();
  m += s1.split('.')[1] ? s1.split('.')[1].length : 0;
  m += s2.split('.')[1] ? s2.split('.')[1].length : 0;
  return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m);
}
// 浮点数加法精确算法
export function accAdd(arg1, arg2) {
  const str1 = arg1.toString();
  const str2 = arg2.toString();
  // 去掉数值里的,号
  const val1 = str1.indexOf(',') != -1 ? str1.replace(/,/g, '') : str1;
  const val2 = str2.indexOf(',') != -1 ? str2.replace(/,/g, '') : str2;
  const r1 = str1.split('.')[1] ? str1.split('.')[1].length : 0;
  const r2 = str2.split('.')[1] ? str2.split('.')[1].length : 0;
  const m = Math.pow(10, Math.max(r1, r2));
  return (val1 * m + val2 * m) / m;
}
// 浮点数减法精确算法
export function accSub(arg1, arg2) {
  const str1 = arg1.toString();
  const str2 = arg2.toString();
  // 去掉数值里的,号
  const val1 = str1.indexOf(',') != -1 ? str1.replace(/,/g, '') : str1;
  const val2 = str2.indexOf(',') != -1 ? str2.replace(/,/g, '') : str2;
  const r1 = str1.split('.')[1] ? str1.split('.')[1].length : 0;
  const r2 = str2.split('.')[1] ? str2.split('.')[1].length : 0;
  const m = Math.pow(10, Math.max(r1, r2));
  // 动态控制精度长度
  const n = (r1 >= r2) ? r1 : r2;
  return ((val1 * m - val2 * m) / m).toFixed(n);
}
// 浮点数除法精确算法
export function accDiv(arg1, arg2) {
  const t1 = arg1.toString().split('.')[1] ? arg1.toString().split('.')[1].length : 0;
  const t2 = arg2.toString().split('.')[1] ? arg2.toString().split('.')[1].length : 0;
  const r1 = Number(arg1.toString().replace('.', ''));
  const r2 = Number(arg2.toString().replace('.', ''));
  return (r1 / r2) * Math.pow(10, t2 - t1);
}